/**
 * 信任博弈游戏主入口文件
 * 整合所有模块并初始化游戏
 */

import { initGame } from './modules/game-core.js';
import { extractResourceValue } from './modules/resources.js';

// 导出资源提取函数到全局，供历史页面使用
window.extractResourceValue = extractResourceValue;

// DOM元素统一收口
const elements = {
    // 统计信息
    playerCoins: document.getElementById('playerCoins'),
    aiCoins: document.getElementById('aiCoins'),
    playerResourcesContainer: document.getElementById('playerResourcesContainer'),
    aiResourcesContainer: document.getElementById('aiResourcesContainer'),

    // 阶段容器
    investmentPhase: document.getElementById('investmentPhase'),
    returnPhase: document.getElementById('returnPhase'),
    resultPhase: document.getElementById('resultPhase'),

    // 投资阶段元素
    investmentSlider: document.getElementById('investmentSlider'),
    investmentValue: document.getElementById('investmentValue'),
    trustInvestmentGroup: document.getElementById('trustInvestmentGroup'),
    trustInvestmentSlider: document.getElementById('trustInvestmentSlider'),
    trustInvestmentValue: document.getElementById('trustInvestmentValue'),
    riskInvestmentGroup: document.getElementById('riskInvestmentGroup'),
    riskInvestmentSlider: document.getElementById('riskInvestmentSlider'),
    riskInvestmentValue: document.getElementById('riskInvestmentValue'),
    trustBonusPreview: document.getElementById('trustBonusPreview'),
    riskPreview: document.getElementById('riskPreview'),
    confirmInvestment: document.getElementById('confirmInvestment'),

    // 返还阶段元素
    investedAmount: document.getElementById('investedAmount'),
    multipliedAmount: document.getElementById('multipliedAmount'),
    returnAmount: document.getElementById('returnAmount'),
    investmentDetails: document.getElementById('investmentDetails'),
    multipliedDetails: document.getElementById('multipliedDetails'),
    returnDetails: document.getElementById('returnDetails'),
    multiplierDisplay: document.getElementById('multiplierDisplay'),
    baseMultiplierDisplay: document.getElementById('baseMultiplierDisplay'),
    trustBonusDisplay: document.getElementById('trustBonusDisplay'),
    riskModifierDisplay: document.getElementById('riskModifierDisplay'),
    returnRateDisplay: document.getElementById('returnRateDisplay'),

    // 结果阶段元素
    playerFinal: document.getElementById('playerFinal'),
    aiFinal: document.getElementById('aiFinal'),
    playerResultResources: document.getElementById('playerResultResources'),
    aiResultResources: document.getElementById('aiResultResources'),
    restartGame: document.getElementById('restartGame'),
    currentRound: document.getElementById('currentRound'),
    totalRounds: document.getElementById('totalRounds'),
    nextRoundInfo: document.querySelector('.next-round-info'),

    // 资源池选择
    poolSelector: document.getElementById('poolSelector'),

    // AI策略选择
    strategySelect: document.getElementById('strategySelect'),
    
    // 玩法介绍模态框元素
    howToPlayModal: document.getElementById('howToPlayModal'),
    openHowToPlay: document.getElementById('openHowToPlay'),
    closeModal: document.querySelector('.close-modal'),
    tutorialSteps: document.querySelectorAll('.tutorial-step'),
    tutorialContents: document.querySelectorAll('.tutorial-content'),
    prevStepBtn: document.getElementById('prevStep'),
    nextStepBtn: document.getElementById('nextStep'),
    startPlayingBtn: document.getElementById('startPlaying')
};

/**
 * 初始化玩法介绍模态框功能
 */
function initTutorial() {
    if (!elements.howToPlayModal) return; // 如果模态框不存在则退出
    
    let currentStep = 1;
    const totalSteps = elements.tutorialSteps.length;
    
    // 打开模态框
    elements.openHowToPlay.addEventListener('click', (e) => {
        e.preventDefault();
        elements.howToPlayModal.style.display = 'block';
        document.body.style.overflow = 'hidden'; // 防止背景滚动
    });
    
    // 关闭模态框
    elements.closeModal.addEventListener('click', () => {
        elements.howToPlayModal.style.display = 'none';
        document.body.style.overflow = ''; // 恢复滚动
    });
    
    // 点击模态框背景关闭
    window.addEventListener('click', (e) => {
        if (e.target === elements.howToPlayModal) {
            elements.howToPlayModal.style.display = 'none';
            document.body.style.overflow = '';
        }
    });
    
    // 切换到指定步骤
    function goToStep(step) {
        // 更新当前步骤
        currentStep = step;
        
        // 更新步骤导航样式
        elements.tutorialSteps.forEach((stepElem) => {
            const stepNum = parseInt(stepElem.getAttribute('data-step'));
            stepElem.classList.remove('active', 'completed');
            
            if (stepNum === currentStep) {
                stepElem.classList.add('active');
            } else if (stepNum < currentStep) {
                stepElem.classList.add('completed');
            }
        });
        
        // 更新内容显示
        elements.tutorialContents.forEach((content) => {
            const contentStep = parseInt(content.getAttribute('data-step'));
            content.classList.toggle('active', contentStep === currentStep);
        });
        
        // 更新按钮状态
        elements.prevStepBtn.disabled = currentStep === 1;
        
        // 最后一步显示"开始游戏"按钮
        if (currentStep === totalSteps) {
            elements.nextStepBtn.style.display = 'none';
            elements.startPlayingBtn.style.display = 'block';
        } else {
            elements.nextStepBtn.style.display = 'block';
            elements.startPlayingBtn.style.display = 'none';
        }
    }
    
    // 步骤导航点击事件
    elements.tutorialSteps.forEach((step) => {
        step.addEventListener('click', () => {
            const stepNum = parseInt(step.getAttribute('data-step'));
            goToStep(stepNum);
        });
    });
    
    // 上一步按钮
    elements.prevStepBtn.addEventListener('click', () => {
        if (currentStep > 1) {
            goToStep(currentStep - 1);
        }
    });
    
    // 下一步按钮
    elements.nextStepBtn.addEventListener('click', () => {
        if (currentStep < totalSteps) {
            goToStep(currentStep + 1);
        }
    });
    
    // 开始游戏按钮
    elements.startPlayingBtn.addEventListener('click', () => {
        elements.howToPlayModal.style.display = 'none';
        document.body.style.overflow = '';
    });
}

// 页面加载完成后初始化游戏和教程
document.addEventListener('DOMContentLoaded', () => {
    initGame(elements);
    initTutorial();
});
